package com.dxy.app.act_widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dxy.app.R;
import com.dxy.app.act_widget.adapter.MyPagerAdapter;
import com.dxy.app.act_widget.adapter.StatusBarAdapter;
import com.dxy.app.act_widget.status_fragment.MainFragment;
import com.dxy.app.act_widget.status_fragment.MineFragment;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dell on 2017/8/8.
 */

public class TranscantStatusBarActivity extends FragmentActivity {

  private ViewPager vp;
  //private StatusBarAdapter adapter;
  private MyPagerAdapter adapter;

  public TranscantStatusBarActivity() {
  }

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_status_bar);

    vp = (ViewPager) findViewById(R.id.vp);

    initFragment();
  }

  //private List<Fragment> list;
  private List<View> list;
  private void initFragment(){
    list =new ArrayList<>();


    /*ViewGroup.LayoutParams lps =new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);

    LinearLayout layout =new LinearLayout(this);
    layout.setLayoutParams(lps);
    layout.setBackgroundColor(Color.YELLOW);

    ImageView im =new ImageView(this);
    im.setImageResource(R.mipmap.i1);

    TextView textView =new TextView(this);*/

    TextView tv =new TextView(this);
    tv.setTextColor(Color.WHITE);
    tv.setText("这是第一页");
    tv.setGravity(Gravity.CENTER);
    tv.setPadding(100,100,100,100);
    tv.setTextSize(60);
    tv.setBackgroundResource(R.mipmap.i1);


    TextView tv1 =new TextView(this);
    tv1.setText("第二页!!!");

    list.add(tv);
    list.add(tv1);

    adapter =new MyPagerAdapter(this,list);

    vp.setAdapter(adapter);

    vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      @Override public void onPageSelected(int position) {
        Toast.makeText(TranscantStatusBarActivity.this, "这是第"+position+"页!!!", Toast.LENGTH_SHORT).show();
      }

      @Override public void onPageScrollStateChanged(int state) {

      }
    });
  }

  public static final int DEFAULT_STATUS_BAR_ALPHA = 112;
  private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.statusbarutil_fake_status_bar_view;
  private static final int FAKE_TRANSLUCENT_VIEW_ID = R.id.statusbarutil_translucent_view;
  private static final int TAG_KEY_HAVE_SET_OFFSET = -123;

  public static void setTranslucentForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
      View needOffsetView) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
      return;
    }
    setTransparentForWindow(activity);
    addTranslucentView(activity, statusBarAlpha);
    if (needOffsetView != null) {
      Object haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET);
      if (haveSetOffset != null && (Boolean) haveSetOffset) {
        return;
      }
      ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams();
      layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity),
          layoutParams.rightMargin, layoutParams.bottomMargin);
      needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true);
    }
  }
  /**
   * 设置透明
   */
  private static void setTransparentForWindow(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
      activity.getWindow()
          .getDecorView()
          .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      activity.getWindow()
          .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
  }

  /**
   * 添加半透明矩形条
   *
   * @param activity       需要设置的 activity
   * @param statusBarAlpha 透明值
   */
  private static void addTranslucentView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
    ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
    View fakeTranslucentView = contentView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);
    if (fakeTranslucentView != null) {
      if (fakeTranslucentView.getVisibility() == View.GONE) {
        fakeTranslucentView.setVisibility(View.VISIBLE);
      }
      fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0));
    } else {
      contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha));
    }
  }
  /**
   * 获取状态栏高度
   *
   * @param context context
   * @return 状态栏高度
   */
  private static int getStatusBarHeight(Context context) {
    // 获得状态栏高度
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    return context.getResources().getDimensionPixelSize(resourceId);
  }
  /**
   * 创建半透明矩形 View
   *
   * @param alpha 透明值
   * @return 半透明 View
   */
  private static View createTranslucentStatusBarView(Activity activity, int alpha) {
    // 绘制一个和状态栏一样高的矩形
    View statusBarView = new View(activity);
    LinearLayout.LayoutParams params =
        new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
    statusBarView.setLayoutParams(params);
    statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
    statusBarView.setId(FAKE_TRANSLUCENT_VIEW_ID);
    return statusBarView;
  }
}
